package simel;

import java.util.*;
import java.io.*;

public class DumbAgent implements Agent
{
    Simulator sim;
    boolean directions[];
    boolean initialMoveFinished[];

    /** Note: Agents must have an empty constructor! **/
    public DumbAgent()
    {
    }

    /** init() will be called before any other method **/
    public void init(Simulator sim)
    {
        this.sim = sim;

        directions = new boolean[sim.getElevators().size()];
        initialMoveFinished = new boolean[sim.getElevators().size()];

        Random r = new Random(0);

        // send each elevator to a random floor to start with.
        for (int i = 0; i < sim.getElevators().size(); i++) {
            Elevator e = sim.getElevators().get(i);
            e.setDestination(e.getFloorMin() + r.nextInt(e.getFloorMax() - e.getFloorMin()));
        }
    }

    // just send the elevators up and down and up and down.
    public void update(Simulator sim, ArrayList<Passenger> passengers, ArrayList<Elevator> elevators)
    {
        for (int i = 0; i < elevators.size(); i++) {
            Elevator e = elevators.get(i);

            // If the elevator still hasn't arrived at its initial starting place, just wait.
            if (!initialMoveFinished[i]) {
                if (e.getFloor() == e.getDestination()) {
                    initialMoveFinished[i] = true;
                    // we've arrived. fall through to usual logic below...
                } else {
                    // keep waiting
                    continue;
                }
            }

            // other wise, ping-pong the elevator up and down.
            if (e.getFloor() == e.getFloorMin())
                directions[i] = true;

            if (e.getFloor() == e.getFloorMax())
                directions[i] = false;

            if (directions[i])
                e.setDestination(e.getFloor() + 1);
            else
                e.setDestination(e.getFloor() - 1);
        }
    }
}
